/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.gui.tbl;

import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import org.tnmk.common.util.BeanUtils;
import org.tnmk.common.util.ImageUtils;
import org.tnmk.worktime.service.action.Action;

/**
 *
 * @author tnmkhoi
 */
public class SelectCellPanel extends javax.swing.JPanel {

  private ImageIcon imgCheckBoxTrue = new ImageIcon(ImageUtils.loadFromClassPath("img/form/checkbox.png"));
  private ImageIcon imgCheckBoxFalse = new ImageIcon(ImageUtils.loadFromClassPath("img/form/empty.png"));
  /**
   * This maybe not the boolean value, it just object which contains necessary
   * boolean value for check-box.
   */
  private Object valueContainer;

  /**
   * Expression to access boolean for check-box inside valueContainer
   */
  private String bolExpression;
  private Action additionalMouseClickAction;

  public SelectCellPanel() {
    constructor("");
  }

  public SelectCellPanel(String bolExpression) {
    this.constructor(bolExpression);
  }

  private void constructor(String bolExpression) {
    initComponents();
    this.bolExpression = bolExpression;
  }

  private void renderSelected(boolean isSelected) {
    if (isSelected) {
      this.jlblCheck.setIcon(imgCheckBoxTrue);
    } else {
      this.jlblCheck.setIcon(imgCheckBoxFalse);
    }
  }

  public void setCheckboxImages(String imgTrue, String imgFalse) {
    imgCheckBoxTrue = new ImageIcon(ImageUtils.loadFromClassPath(imgTrue));
    imgCheckBoxFalse = new ImageIcon(ImageUtils.loadFromClassPath(imgFalse));
  }

  public boolean isSelected() {
    Object bol = BeanUtils.getValue(valueContainer, bolExpression);
    return (Boolean) bol;
  }

  public void addSelectMouseListener(MouseListener mouseAdapter) {
    this.jlblCheck.addMouseListener(mouseAdapter);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jlblCheck = new javax.swing.JLabel();

    setOpaque(false);

    jlblCheck.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jlblCheck.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/form/checkbox_btn.png"))); // NOI18N
    jlblCheck.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        jlblCheckMouseClicked(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jlblCheck, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jlblCheck, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
  }// </editor-fold>//GEN-END:initComponents

  private void jlblCheckMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jlblCheckMouseClicked
//    LogUtils.printCaller(this);
    clickCheckBox();
  }//GEN-LAST:event_jlblCheckMouseClicked

  public void clickCheckBox() {
//    LogUtils.printCaller(this);
    autoChangeSelection(this, this.bolExpression, this.valueContainer);
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  protected javax.swing.JLabel jlblCheck;
  // End of variables declaration//GEN-END:variables

  public Action getAdditionalMouseClickAction() {
    return additionalMouseClickAction;
  }

  public void setAdditionalMouseClickAction(Action mouseClickAction) {
    this.additionalMouseClickAction = mouseClickAction;
  }

  void setCheckboxValueContainer(Object obj) {
    this.valueContainer = obj;
  }

  void renderCheckbox() {
    Boolean selected = isSelected();
    this.renderSelected(selected);
  }

  /**
   * This method will change select status of check-box automatically. And it
   * also change corresponding boolean value (change from checked to un-check,
   * and vise versa)
   *
   * @param editorComp
   * @param bolExpression
   * @param valueContainer
   */
  public static void autoChangeSelection(SelectCellPanel editorComp, String bolExpression, Object valueContainer) {
    Object val = BeanUtils.getValue(valueContainer, bolExpression);
    Boolean selected = (Boolean) val;
    Boolean newSelected = !selected;
    Object changedValue = BeanUtils.setValue(valueContainer, bolExpression, newSelected);
    editorComp.setCheckboxValueContainer(changedValue);
    editorComp.renderCheckbox();
    Action mouseClickAction = editorComp.getAdditionalMouseClickAction();
    if (mouseClickAction != null) {
      mouseClickAction.perform();
    }
  }
}
